package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.GroupActive;
import com.bwie.pojo.GroupJoin;
import com.bwie.service.GroupJoinService;
import com.bwie.mapper.GroupJoinMapper;
import com.bwie.utils.R;
import com.bwie.vo.JoinVo;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 陶仕豪
 * @description 针对表【tb_group_join(商家拼团活动表)】的数据库操作Service实现
 * @createDate 2025-07-27 19:06:34
 */
@Service
public class GroupJoinServiceImpl extends ServiceImpl<GroupJoinMapper, GroupJoin>
        implements GroupJoinService {

    @Autowired
    GroupJoinMapper groupJoinMapper;
    @Autowired
    HttpServletRequest request;

    @Override
    public R getGroupJoinList(JoinVo joinVo) {
        Page<GroupJoin> joinPage = new Page<>(joinVo.getPageNum(), joinVo.getPageSize());

        Page<GroupJoin> page = groupJoinMapper.getActiveJoinList(joinPage, joinVo);
        for (GroupJoin record : page.getRecords()) {
            ArrayList<String> list = new ArrayList<>();
            List<String> images= groupJoinMapper.selectImage(record.getJoinId());
           if(images!=null&&images.size()>0){
               for (String image : images) {
                   if(image!=null){
                       List<String> strings = JSON.parseArray(image, String.class);
                       list.addAll(strings);
                   }

               }
           }
           record.setImageList(list);
        }
        return R.OK(page);
    }

    @Override
    public R addGroupJoin(GroupJoin groupJoin) {
        String token = request.getHeader("token");
        if (token == null) {
            return R.ERROR(500, "请先登录");
        }
        String userName = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userName", String.class);

            GroupJoin groupJoinOne = groupJoinMapper.selectOne(new QueryWrapper<GroupJoin>().lambda().eq(GroupJoin::getJoinName, groupJoin.getJoinName()));
            if(groupJoinOne!=null){
                return R.ERROR(500,"拼团名称不能重复");
            }

        long beginTime = groupJoin.getBeginTime().getTime();
        long endTime = groupJoin.getEndTime().getTime();
        if(endTime<beginTime){
            return R.ERROR(500,"截止时间不能小于开始时间");
        }
        groupJoin.setCreateBy(userName);
        groupJoin.setCreateTime(new Date());
        groupJoinMapper.insert(groupJoin);
        return R.OK();
    }

    @Override
    public R updateGroupJoin(GroupJoin groupJoin) {
        String token = request.getHeader("token");
        if (token == null) {
            return R.ERROR(500, "请先登录");
        }
        String userName = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userName", String.class);
        groupJoin.setUpdateBy(userName);
        if(groupJoin.getImageList()!=null&&groupJoin.getImageList().size()>0){
            groupJoin.setJoinImages(JSON.toJSONString(groupJoin.getImageList()));
        }
        groupJoin.setUpdateTime(new Date());
        groupJoinMapper.updateById(groupJoin);
        return R.OK();
    }

    @Override
    public R deleteGroupJoin(GroupJoin groupJoin) {
        GroupJoin groupJoin1 = groupJoinMapper.selectById(groupJoin.getJoinId());
        if (groupJoin1 == null) {
            return R.ERROR(500, "活动不存在");
        }
        groupJoinMapper.deleteById(groupJoin);
        return R.OK();
    }

    @Override
    public R deleteAllGroupJoin(List<GroupJoin> groupJoinList) {
        if (groupJoinList == null || groupJoinList.isEmpty()) {
            return R.ERROR(500, "请选择活动");
        }
        for (GroupJoin groupJoin : groupJoinList) {
            groupJoinMapper.deleteById(groupJoin.getJoinId());
        }
        return R.OK();

    }
}




